package com.thirtydays.frame.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import coil.load
import coil.request.ImageRequest
import coil.transform.RoundedCornersTransformation
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.interfaces.OnCallbackListener
import com.luck.picture.lib.utils.ActivityCompatHelper
import com.thirtydays.frame.R
import java.io.File

/**
 * @author 小垚
 * @创建时间： 2020/12/3
 * @描述：图片加载库
 **/
class CoilEngine private constructor() : ImageEngine {

    companion object {
        val INSTANCE: CoilEngine by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            CoilEngine()
        }
    }

    /**
     *加载图片
     */
    override fun loadImage(context: Context, url: String, imageView: ImageView) {
        imageView.load(url) {
            crossfade(true)
            placeholder(R.mipmap.ic_launcher)
            error(R.mipmap.ic_launcher)
        }
    }

    /**
     *加载图片
     */
    fun loadImage(context: Context, url: Any, imageView: ImageView, placeholder: Int = -1, error: Int = -1) {
        imageView.load(url) {
            crossfade(true)
            placeholder(placeholder)
            error(error)
        }
    }

    /**
     *加载图片
     */
    fun loadRoundedImage(context: Context, url: Any, imageView: ImageView, rounded: Float, placeholder: Int = -1, error: Int = -1) {
        imageView.load(url) {
            crossfade(true)
            placeholder(placeholder)
            error(error)
            transformations(RoundedCornersTransformation(rounded))
        }
    }

    /**
     *加载图片
     */
    fun loadRoundedImage(context: Context, url: File, imageView: ImageView, TLRounded: Float, TRRounded: Float, BLRounded: Float, BRRounded: Float,
                         placeholder: Int = -1, error: Int = -1) {
        imageView.load(url) {
            crossfade(true)
            placeholder(placeholder)
            error(error)
            transformations(RoundedCornersTransformation(TLRounded, TRRounded, BLRounded, BRRounded))
        }
    }


    override fun loadImageBitmap(
        context: Context,
        url: String,
        maxWidth: Int,
        maxHeight: Int,
        call: OnCallbackListener<Bitmap>?
    ) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        ImageRequest.Builder(context)
            .data(url)
            .target {
                call?.run {
                    onCall((it as BitmapDrawable).bitmap)
                }
            }
            .build()

    }

    override fun loadAlbumCover(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return;
        }
        ImageRequest.Builder(context)
            .data(url)
            .size(180,180)
            .target {
                val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.resources, (it as BitmapDrawable).bitmap)
                circularBitmapDrawable.cornerRadius = 8f
                imageView.setImageDrawable(circularBitmapDrawable)
            }
            .build()

    }

    override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return;
        }
        imageView.load(url){
            size(200,200)
        }

    }

    override fun pauseRequests(context: Context) {

    }

    override fun resumeRequests(context: Context) {

    }
}